/**
 *File IO
 *@author Roxanne
 *@version 1.0 	10.04.13
 *  
 */
	
	/**
     * Saves the game to the MuleData.dat
     * @param saveSlot the slot to save to
     * @return true if successful
     */
    private boolean saveToFile(int saveSlot){
    	
        File save = new File("MuleData.dat"); 
        //This file will contain things like the user's name, and option preferences.
        //Things like Sound Volume, Music Volume, Key Bindings, Username, Last connected IP address
        if(!save.exists()){ 
        	save.createNewFile();
            try{
                save.createNewFile();
            }catch(Throwable t){
                
            }
        }
        try{
            FileReader fr = new FileReader(save);
            BufferedReader br = new BufferedReader(fr);
            String line1 = br.readLine();
            String line2 = br.readLine();
            String line3 = br.readLine();
            String line4 = br.readLine();
            if(line1 == null){
                line1 = "";
            }
            if(line2 == null){
                line2 = "";
            }
            if(line3 == null){
                line3 = "";
            }
            if(line4 == null){
                line4 = "";
            }
            FileWriter writer = new FileWriter(save, false);
            BufferedWriter out = new BufferedWriter(writer);
            String data = field.getLevel() + "-"	 
                    + field.getScore() + "-"	
                    + field.getLinesCleared() + "-"
                    + timeS + "-"
                    + timeM + "-"
                    + (field.getActivePiece().tileID-1) + "-"
                    + field.getNextPieces().get(0)
                    + field.getNextPieces().get(1)
                    + field.getNextPieces().get(2)
                    + field.getNextPieces().get(3)
                    + "-";
            //So this here was me saving the state of the game. Things like score, lines cleared, the next
            //pieces that were generated and ready to fall. You'll put whatever needs to be in this file
            //in here as a series of strings, one for each line of the file.
            //Then you'll read them out in the same order you put them in.
            
            
            //The bottom method is for reading out the strings that were saved in. It assumes they are in the
            //same order as they were placed in, so if someone tampers with it, and it makes a good file
            //then it will just load it as is.
            field.getActivePiece().phaseOut();
            for(int i = 0; i < PlayingField.BOARD_HEIGHT; i++){
                for(int k = 0; k < PlayingField.BOARD_WIDTH; k++){
                    data+=""+field.getBoardStatus()[k][i];
                }
            }
            field.getActivePiece().phaseIn();
            switch(saveSlot){
                case 1:
                    out.write(line1);
                    out.newLine();
                    out.write(data);
                    out.newLine();
                    out.write(line3);
                    out.newLine();
                    out.write(line4);
                    break;
                case 2:
                    out.write(line1);
                    out.newLine();
                    out.write(line2);
                    out.newLine();
                    out.write(data);
                    out.newLine();
                    out.write(line4);
                    break;
                case 3:
                    out.write(line1);
                    out.newLine();
                    out.write(line2);
                    out.newLine();
                    out.write(line3);
                    out.newLine();
                    out.write(data);
                    break;
            }
            out.flush();
            out.close();
            return true;
        }catch(IOException e){
            System.out.println(e.getLocalizedMessage());
            return false;
        }
    }

    
    /**
     * Loads the game from MuleData.dat if it exists.
     * @param saveSlot the slot to load from
     * @return true if successful
     */
    private boolean loadFromFile(int saveSlot){
        File save = new File("MuleData.dat");
        if(!save.exists()){
            return false;
        }
        try{
            FileReader fr = new FileReader(save);
            BufferedReader br = new BufferedReader(fr);
            String cf = br.readLine();
            String s1 = br.readLine();
            String s2 = br.readLine();
            String s3 = br.readLine();
            String[] data = new String[0];
            switch(saveSlot){
                case 1:
                    data = s1.split("-");
                    break;
                case 2:
                    data = s2.split("-");
                    break;
                case 3:
                    data = s3.split("-");
                    break;
            }

            if(data[0].length() > 2 || data[1].length() > 5 || data[2].length() > 2 || data[3].length() > 2 || data[4].length() > 3 || data[5].length() != 1 || data[6].length() != 4 || data[7].length() != PlayingField.BOARD_WIDTH*PlayingField.BOARD_HEIGHT){
                return false;
            }
            int level = new Integer(data[0]);
            int score = new Integer(data[1]);
            int lines = new Integer(data[2]);
            int timeS = new Integer(data[3]);
            int timeM = new Integer(data[4]);
            int currentPiece = new Integer(data[5]);
            if(currentPiece > 6){
                return false;
            }
            int[] next = new int[]{new Integer(data[6].substring(0, 1)), new Integer(data[6].substring(1, 2))
                                 , new Integer(data[6].substring(2, 3)), new Integer(data[6].substring(3, 4))};
            if(next[0]>6 || next[1]>6 || next[2]>6 || next[3]>6){
                return false;
            }
            int[][] boardStatus = new int[PlayingField.BOARD_WIDTH][PlayingField.BOARD_HEIGHT];
            int prog = 0;
            for(int i = 0; i < PlayingField.BOARD_HEIGHT; i++){
                for(int k = 0; k < PlayingField.BOARD_WIDTH; k++){
                    boardStatus[k][i] = new Integer(data[7].substring(prog, prog+1));
                    if(boardStatus[k][i] > 7)
                        return false;
                    prog++;
                }
            }
            if(timeS > 59)
                return false;
            this.timeS = timeS;
            this.timeM = timeM;
            field = new PlayingField(20, 75, level, score, lines, currentPiece, next, boardStatus);
            br.close();
            fr.close();
            return true;
        }catch(Throwable t){
            return false;
        }
    }